<?php
require_once(dirname(__FILE__)."/../lib/Loader.php");
cfgload("defines");
cfgload("dd");
cfgload("distr");
cload("Smarty.smartyRenderer");
cload("requestProcessor");
cload("dd.ddrow");
cload("distr.distribution");
cload("kinopoisk.kinoPoiskFilm");
/**
 * Description of ddForm
 *
 * @author Di_Moon
 */
class ddForm extends smartyRenderer {
    protected $_dbh;
    protected $_ddrow;
    protected $_ddFor;
    protected $_rowId;
    protected $_reqId;
    
    protected $_film;
    protected $_distr;
    
    function __construct($dbh) {
        parent::__construct();
        $this->_dbh = $dbh;
        $this->_ddFor = requestProcessor::getVar("for");
        $this->_reqId = requestProcessor::getVar("id");
        $this->_rowId = $this->_ddFor.":".$this->_reqId;
        
        $this->_ddrow = new ddrow($this->_dbh, $this->_ddFor, $this->_rowId);
        
        $this->assign("ddfor", $this->_ddFor);
        $this->assign("ddid", $this->_reqId);
        $this->assign("distrTypes", _distr_invertTypes());
        
        if($this->_ddFor == DOPDATA_FOR_FILM) {
            $this->_film = new kinoPoiskFilm($this->_dbh, $this->_reqId);
        }
        if($this->_ddFor == DOPDATA_FOR_DISTR) {
            $this->_distr = new distribution($this->_dbh, $this->_reqId);
            $this->_distr->load();
            $this->_film = new kinoPoiskFilm($this->_dbh, $this->_distr->get("filmid"));
            $this->assign("distrInfo", $this->_distr->getElements());
        }
    
        $this->assign("filmInfo", $this->_film->getElements());

    }
    
    function render() {
        $action = requestProcessor::getVar("action");
        if($action=="form") {
            $post = requestProcessor::post();
            
            foreach($post as $_field=>$_value) {
                $this->_ddrow->set($_field, $_value);
            }
            
            $this->_ddrow->save();
        }
        $_formData = array();
        // _print_r($this->_ddrow->getFields());
        foreach($this->_ddrow->getFields() as $key=>$row) {
            $_formData[$row->fld_name] = $this->_ddrow->getDefaultData($row->fld_name);
        }
        foreach($this->_ddrow->getData() as $key=>$value) {
            if($value) $_formData[$key] = htmlspecialchars($value);
        }
        
        $this->assign("formFields", $this->_ddrow->getFields());
        $this->assign("formData", $_formData);
        
        return $this->fetch("ddForm.tpl");
    }
}

?>
